package 结构型模式.代理模式.动态代理;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Date;

interface ISwitch
{
	void on();
}

class Tv implements ISwitch
{
	@Override
	public void on() {
		System.out.println("打开电视机");
	}
}

class ProxyTv implements InvocationHandler
{
	private Object target;

	public ProxyTv(Object target) 
	{
		this.target = target;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable 
	{
		System.out.println("执行日志");

		Object result = method.invoke(target, args);
		
		System.out.println("结束日志");
		return result;
	}
}


public class DynamicProxy 
{
	public static void main(String[] args) 
	{
		ISwitch tv = new Tv();
		ProxyTv proxyTv = new ProxyTv(tv);

		ISwitch o = (ISwitch)Proxy.newProxyInstance(tv.getClass().getClassLoader(), tv.getClass().getInterfaces(), proxyTv);
		
		o.on();
	}
}
